<?php


namespace app\shop\model;

use app\admin\model\Common;


/**
 * Class Region
 * @package app\shop\model
 * @author Mr.Li
 * @create 2021-05-31 10:42
 */
class Region extends Common
{
    /**
     * 表名
     * @var string
     */
    protected $name = 'shop_region';

    /**
     * 根据id获取下级数据
     * @param array $request
     * @return array
     * @author Mr.Li
     * @create 2021-06-07 15:00
     */
    public function getDataListById(array $request): array
    {
        $pid = intval($request['pid']) ?? 0;
        try {
            $list = $this->where('id', '>', 0)->where('parent_id','=',$pid)->select()->toArray();
            return $list;
        }catch (\Exception $e) {
            // throwException($e->getMessage());
            $this->error = '获取区域异常';
            $this->error = $e->getMessage();
            return [];
        }
    }
    /**
     * 三级区域列表
     * @param array $request
     * @return array
     * @author Mr.Li
     * @create 2021-06-01 9:26
     */
    public function getDataList(array $request): array
    {
        $firstFloor = $secondFloor = $thirdFloor = [];
        try {
            $list = $this->where('id','>',0)->select()->toArray();
            if($list){
                foreach($list as $v){
                    if($v['level'] == 1){
                        $firstFloor[] = $v;
                    }elseif($v['level'] == 2){
                        $secondFloor[$v['parent_id']][] = $v;
                    }elseif($v['level'] == 3){
                        $thirdFloor[$v['parent_id']][] = $v;
                    }
                }
                foreach($firstFloor as &$item)
                {
                    $item['son'] = isset($secondFloor[$item['id']]) ? $secondFloor[$item['id']] : [];
                    if($item['son']){
                        foreach($item['son'] as &$itemSon)
                        {
                            $itemSon['son'] = isset($thirdFloor[$itemSon['id']]) ? $thirdFloor[$itemSon['id']] : [];
                        }
                    }

                }

            }
            return $firstFloor;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return [];
        }
    }



    /**
     * 获取多层区域
     * @param array $arr
     * @param string $key
     * @param string $pidKey
     * @param int $pid
     * @return array
     * @author Mr.Li
     * @create 2021-06-01 11:06
     */
    public static function recursionTree($arr = [],$key = 'id',$pidKey = 'parent_id',$pid = 0)
    {
        $list = [];
        foreach($arr as $item){
            if($item['level'] < 4){
                if($item[$pidKey] == $pid){
                    $child = self::recursionTree($arr,$key,$pidKey,$item[$key]);
                    if($child){
                        $item['child'] = $child;
                    }
                    $list[] = $item;
                }
            }

        }
        return $list;
    }

    /**
     * 新增区域
     * @param array $request
     * @return bool
     * @author Mr.Li
     * @create 2021-05-30 8:51
     */
    public function addData(array $request): bool
    {
        try {

            // 验证
            $validate = new \app\shop\validate\Region();
            if (!$validate->scene('create')->check($request)) {
                $this->error = $validate->getError();
                return false;
            }
            $data = [
                'name'                  => trim($request['name']),
                'create_user_id'        => session('user_id'),
                'created_at'            => time(),
                'updated_at'            => time(),
            ];
            $id = $this->insertGetId($data);
            if ($id) {
                $code = "DXJ".str_pad($id,4,"0",STR_PAD_LEFT);
                $this->where('id',$id)->save(array('code'=>$code));
                $this->error = '添加成功';
                return true;
            }
        } catch (\Exception $e) {
        }
        $this->error = '添加失败';
		return false;
    }

    /**
     * 更新区域
     * @param array $request
     * @return bool
     * @author Mr.Li
     * @create 2021-05-30 8:51
     */
    public function saveData(array $request): bool
    {
        try {
            $id = $request['id'] ?? 0;
            if (!$id) {
                $this->error = '参数错误';
                return false;
            }
            // 验证
            $validate = new \app\shop\validate\Warehouse();
            if (!$validate->scene('update')->check($request)) {
                $this->error = $validate->getError();
                return false;
            }

            $data = [
                'name'        => trim($request['name']),
               // 'unit_id'     => $request['unit_id'],
                'updated_at' => time(),
            ];

            $bool = $this->where(['id'=>$id])->update($data);
            if ($bool) {
                $this->error = '更新成功';
                return true;
            }
        } catch (\Exception $e) {

        }
        $this->error = '更新失败';
		return false;
    }


}
